//
//  USTarotAskLoadingView.m
//  us
//
//  Created by karry on 2025/5/16.
//

#import "USTarotAskLoadingView.h"
#import "USTarotLoadingView.h"

@interface USTarotAskLoadingView()

@property (nonatomic,strong)NSTimer *timer;

@property (nonatomic,strong) USTarotLoadingView *loadingView;

@end

@implementation USTarotAskLoadingView


+ (USTarotAskLoadingView *)show{
    
    UIWindow *window = UIWindow.keyWindow;
    USTarotAskLoadingView *view = [[USTarotAskLoadingView alloc]initWithFrame:window.bounds];
    [window addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    
    return view;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupView];
    }
    return self;
}

- (void)setupView{
    
    self.backgroundColor = UIColor.blackColor;
    
    
    USTarotLoadingView *loadView = [USTarotLoadingView jk_loadInstanceFromNib];
    _loadingView = loadView;
    [self addSubview:loadView];
    [loadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.centerY.mas_equalTo(self);
    }];
    
    NSArray<NSString *> *waitingMessages = @[
        @"星辰已在低语，命运的轮盘缓缓转动中……",
        @"塔罗正在与宇宙共振，请稍候……",
        @"施法中……请勿打扰占卜师的灵感之流。",
        @"月亮正低语，卡牌即将显现它的秘密。",
        @"命运之门微启，真相正穿越星界而来……",
        @"屏气凝神，整合能量",
        @"正在连线占星服务器……同步星体轨迹中……",
        @"🌌 正在翻阅前世卷轴……请稍等片刻。"
    ];
    
    // 随机获取一句
    NSString *randomMessage = waitingMessages[arc4random_uniform((uint32_t)waitingMessages.count)];
    loadView.infoLab.text = randomMessage;
    
    NSString *gifPath = [[NSBundle mainBundle] pathForResource:@"Frame 1430104974" ofType:@"gif"];
    [loadView.loadImageView sd_setImageWithURL:[NSURL fileURLWithPath:gifPath]];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(createMeteorImageViewGroup) userInfo:nil repeats:YES];
    [self createMeteorImageViewGroup];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[[UIImage imageNamed:@"ic_close"] imageWithTintColor:UIColor.whiteColor] forState:0];
    [closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:closeBtn];
    
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.top.mas_equalTo(self.mas_safeAreaLayoutGuideTop).mas_offset(20);
    }];
    
}

- (void)setCard:(USTarotCard *)card{
    _card = card;
    
    self.loadingView.titleLab.text = card.name;
    self.loadingView.subLab.text = card.orientation == USTarotCardOrientationUpright?@"正位":@"逆位";
    
}

- (void)closeAction{
    
    [USFeedbackTool impactSoft];
    [self dissmiss];
    
}

- (void)dissmiss{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
    
}

- (void)layoutSubviews {
    [super layoutSubviews];

}
- (void)createMeteorImageViewGroup {
    for (int i = 0; i < 6; i++) {
        CGFloat r = drand48(); // 返回一个 [0.0, 1.0) 的 double 值
        [self createSingleMeteorImageViewWithDelay:r];
    }
}

- (void)createSingleMeteorImageViewWithDelay:(NSTimeInterval)delay {
 
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        UIImage *meteorImage = [[UIImage imageNamed:@"Group 2146"] imageWithTintColor:UIColor.jk_randomMacaronColor];
//        UIImage *meteorImage  = [UIImage imageNamed:@"Group 2146"];
        UIImageView *meteorView = [[UIImageView alloc] initWithImage:meteorImage];
        

        CGFloat size = meteorImage.size.width; // 随机大小
        CGFloat xOffset = arc4random_uniform(self.bounds.size.width * 0.5) + self.bounds.size.width * 0.5;
        CGFloat yOffset = -size;
        meteorView.frame = CGRectMake(xOffset, -size, size, size);
        meteorView.alpha = 0.8;
        [self addSubview:meteorView];
        
        CGFloat xEnd = -size;
        CGFloat deltaX = xEnd - xOffset;
        CGFloat deltaY = -deltaX;
        CGFloat yEnd = yOffset + deltaY;
   
        CGPoint endPoint = CGPointMake(-size, yEnd);
        
        [UIView animateWithDuration:3.f
                              delay:0
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^{
            meteorView.center = endPoint;
            meteorView.alpha = 0.0;
        } completion:^(BOOL finished) {
            [meteorView removeFromSuperview];
        }];
        
    });
    

    
}


@end
